package com.hulk.boot.common.util;

import static com.google.common.base.Preconditions.checkArgument;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;

/**
 * @author 
 * 
 */
@Slf4j
public class HttpClientUtil {

	 /*
	private HttpClientUtil() {
	}
	
	@SuppressWarnings("unused")
	private static RequestConfig config  ;

	private static HttpClientUtil httpClientUtil;

	
	
	 public static HttpClientUtil createHttpClientUtil  ()  {
		config = RequestConfig.custom().setConnectTimeout(60000)
				.setSocketTimeout(60000).build();
		httpClientUtil = new HttpClientUtil();
		return httpClientUtil;
	}
	
	public static HttpClientUtil createHttpClientUtil(int connTimeout,
			int reqTimeout) {
		config = RequestConfig.custom().setConnectTimeout(connTimeout)
				.setSocketTimeout(reqTimeout).build();
		httpClientUtil = new HttpClientUtil();
		return httpClientUtil;
	}
 
	public static HttpClientUtil createHttpClientProxyUtil(int connTimeout,
			int reqTimeout, String proxyHost, int proxyPort) {
		config = RequestConfig.custom().setConnectTimeout(connTimeout)
				.setSocketTimeout(reqTimeout)
				.setProxy(new HttpHost(proxyHost, proxyPort))
				.setAuthenticationEnabled(true).build();
		httpClientUtil = new HttpClientUtil();
		return httpClientUtil;
	}*/
	public static  String sendByPost(String url,   Map<String, String> param){
		   return sendByPost( url, 60000, 60000,   param);
	}
  
	public static String sendByPost(String url, int connTimeout, int reqTimeout,  Map<String, String> param) {
		
		RequestConfig config = RequestConfig.custom().setConnectTimeout(connTimeout)
				.setSocketTimeout(reqTimeout).build();
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = null;
		CloseableHttpResponse response = null;
		try {
			httpPost = new HttpPost(url);
			List<NameValuePair> paraList = Lists.newArrayList();
			for (String key : param.keySet()) {
				paraList.add(new BasicNameValuePair(key, param.get(key)));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(paraList, Consts.UTF_8));
			httpPost.setConfig(config);
			response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			int statusCode = response.getStatusLine().getStatusCode();
			checkArgument(Objects.equal(statusCode, HttpStatus.SC_OK),
					"响应码状态不是200");
			// httpPost.clone();
			return EntityUtils.toString(entity);
		} catch (Exception e) {
			log.error("调用HTTP post 服务异常，{}", e);
			return "";
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
					log.info("", e);
					
				}
			}
			if (httpPost != null) {
				httpPost.releaseConnection();
			}
			if (httpClient != null) {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
					log.info("", e);
				}
			}

		}
	}

	public  static String sendByGet(String url,   Map<String, String> param){
		   return sendByPost( url, 60000, 60000,   param);
	}
	
	public static  String sendByGet(String url, int connTimeout, int reqTimeout, Map<String, String> param) {
		
		RequestConfig config = RequestConfig.custom().setConnectTimeout(connTimeout)
				.setSocketTimeout(reqTimeout).build();
		
		HttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = null;
		HttpResponse response = null;
		try {
			
			StringBuffer paramUrl = new StringBuffer();
			for (String key : param.keySet()) {
				paramUrl.append("&").append(key).append("=")
						.append(param.get(key));
			}
			
			httpGet = new HttpGet(url + paramUrl);
			httpGet.setConfig(config);
			response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			int statusCode = response.getStatusLine().getStatusCode();
			checkArgument(Objects.equal(statusCode, HttpStatus.SC_OK),
					"响应码状态不是200");
			return EntityUtils.toString(entity);
		} catch (Exception e) {
			log.error("调用HTTP get 服务异常，{}", e);
			return "";
		} finally {
			if (httpGet != null) {
				httpGet.releaseConnection();
			}

		}
	}
	
	public static void main(String[] args) {
		sendByGet("127.0.0.1", new HashMap<String, String>());
	}
}
